var ValidationUtil = {

		isBlank: function(v){
            return  ((v == null) || (v.length == 0));
        },

        isNumber:function(v){
            return this.isBlank(v) || !(/^\d+(\.\d+)?$/.test(v));
        },

        isDigits:function(v){
            return this.isBlank(v) || /[^\d]/.test(v);
        },

        isEmail:function(v){
            return  /^[a-z0-9_\-\.]+@[a-z0-9_\-\.]+\.[a-z]{2,4}$/.test(v);
        },

        isDate:function(v){
            return this.isBlank(v) || !/Invalid|NaN/.test(new Date(v));
        },

        checkRangeValue : function(v,paramMin,paramMax){
            return !(v >= paramMin && v <= paramMax) ;
        },

        showErrMsg:function(node,msg){
            $(node).innerHTML = msg;
        },

        clearErrMsg:function(node){
            $(node).innerHTML = '';
        },

        /*
        * new add to validate is letter and not blank
        */
        isLetter: function(v){
            return  this.isBlank(v) || !(/^[a-zA-Z]+$/.test(v));
        },

        isInteger: function(v){
            return  /^[1-9]+[0-9]*]*$/.test(v);
        },

        isUserName: function(v){
            return  /^[a-zA-Z\u4E00-\u9FA5]{1,1}[a-zA-Z0-9\u4E00-\u9FA5\_]{1,11}/.test(v);
        },
        validataPsw : function(v){
            return /(\d+[a-zA-Z]+)|([a-zA-Z]+\d+)/.test(v);
        },
        isMobilePhone : function(v){
            return  /^(\+*\d{2})*1\d{10}$/.test(v);
        }
        /**
         * @todo validateStringLength( strString, intLengthMax, [intLengthMin] )
         */
}

var CampusApply = {
    checkForm : function(){
        var boolStatus = true;
        for( strFunctionName in this.validateData )
            if( ! this.validateData[strFunctionName]() )
                boolStatus = false;
        if( !boolStatus )
            return false;
    },
    throwException : function( obj ){
    	$( '#' + obj.id + 'Memo' ).addClass('error_font_color');
        $( '#' + obj.id + 'Memo' ).html( $( '#' + obj.id + 'Msg' ).val() );
    },
    throwNiceMsg : function( obj ){
    	$( '#' + obj.id + 'Memo' ).removeClass('error_font_color');
        $( '#' + obj.id + 'Memo' ).html( $( '#niceMsg' ).val() );
    },
    validateData: {
        validateUserId : function(){
            var obj = $( '#username' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                $.ajax({
                     type: 'post',
                     dataType: "json",
                     url: '/a/validateusername',
                     data: {"username" : $.trim( obj.value ) },
                     async: false,  //false is synchronization , true is asynchronism
                     success: function(data){
                         if(!data.result){
                            CampusApply.throwNiceMsg( obj );
                            return true;
                         }else{
                             CampusApply.throwException( obj );
                             return false;
                         }
                     },
                     error: function(){
                         CampusApply.throwException( obj );
                         return false;
                     }
                });
                return true;
            }
        },
        validateEmail : function(){
            var obj = $( '#email' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) || !ValidationUtil.isEmail( obj.value ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateFullname : function(){
            var obj = $( '#fullname' ).get()[0];
            if( ValidationUtil.isBlank( obj.value ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateAge : function(){
            var obj = $( '#age' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) || ValidationUtil.isNumber( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validatePostalCode : function(){
            var obj = $( '#postal_code' ).get()[0];
            if( !ValidationUtil.isBlank( $.trim( obj.value ) ) && ValidationUtil.isNumber( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateSchool : function(){
            var obj = $( '#school' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateDepartment : function(){
            var obj = $( '#department' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateZhuanye : function(){
            var obj = $( '#zhuanye' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateStudentId : function(){
            var obj = $( '#student_id' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateAddress : function(){
            var obj = $( '#address' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateDormitoryAddress : function(){
            var obj = $( '#dormitory_address' ).get()[0];
            if( ValidationUtil.isBlank( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateDentityId : function(){
            var obj = $( '#dentity_id' ).get()[0];
            if( !/^\d{17}[xX\d]{1}$/.test( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateQQ : function(){
            var obj = $( '#qq' ).get()[0];
            if( !/^\d{5,18}$/.test( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        },
        validateMobile : function(){
            var obj = $( '#mobile' ).get()[0];
            if( !ValidationUtil.isMobilePhone( $.trim( obj.value ) ) ){
                CampusApply.throwException( obj );
                return false;
            }else{
                CampusApply.throwNiceMsg( obj );
                return true;
            }
        }
    }
}

var AreaDict = {
        changeArea : function(level){
            var parentId;
            if(level==2){
                parentId = $('#province').val();
            }else if(level==3){
                parentId = $('#city').val();
            }
          //  alert(parentId);
            var defaultOption = $('#defaultOption').val();

            $.ajax({
                 type: 'post',
                 dataType: "json",
                 url: '/u/getareadict',
                 data: {"parentId" : parentId, "level":level},
                 async: false,  //false is synchronization , true is asynchronism
                 success: function(data){

                     if(data.length){
                            var i = 0;
                            var defaultOpt = '<option value="">'+defaultOption+'</option>';
                            var html = defaultOpt;

                            var dict;
                            for(i;i<data.length;i++){

                                dict = '<option value="'+data[i].id+'">'+data[i].title+'</option>';
                                html += dict;
                            }

                            if(level==2){
                                $('#city').html(html);
                                $('#county').html(defaultOpt);
                                $('#city').show();
                                $('#county').hide();

                            } else if(level==3) {
                                $('#county').html(html);
                                $('#county').show();
                            }
                        } else {
                            if(level==2){
                                $('#city').html(defaultOpt);
                                $('#county').html(defaultOpt);
                                $('#city').hide();
                                $('#county').hide();

                            } else if(level==3) {
                                $('#county').html(defaultOpt);
                                $('#county').hide();
                            }
                        }
                 },
                 error: function(){

                      result = false;
                    }
                });
        },
        validateAreaDict : function(){
            var result = false;
            var province = $('#province').val();
            var city = $('#city').val();
            var county = $('#county').val();
            if(!province || !city || !county){
                var errorArea = $('#errorAreaEmpty').val();
                $('#addressnMemo').addClass('error_font_color');
                $('#addressnMemo').html(errorArea);

            } else {
            	$('#addressnMemo').removeClass('error_font_color');
                $('#addressnMemo').html('');
                result = true;
            }
            return result;
        }
}
